<template>
  <div>
    <div class="card" style="margin-bottom: 10px;">
      <el-input v-model="name" prefix-icon="Search" style="width: 300px; margin-right: 10px" placeholder="请输入订单名称查询"></el-input>
      <el-button type="primary" @click="load(1)">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px">
      <el-table :data="tableData" style="width: 100%" stripe :header-cell-style="{backgroundColor: 'aliceblue', color: '#666'}" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>

        <el-table-column prop="goodsName" label="订单名"/>
        <el-table-column prop="picture" label="订单图片">
          <template v-slot="scope">
            <img :src="scope.row.picture || 'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png'" alt="" style="width: 50px; height: 50px; border-radius: 50%">
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="下单用户"/>
        <el-table-column prop="number" label="订单数量"/>
        <el-table-column prop="price" label="订单价格"/>
        <el-table-column prop="content" label="订单详情">
          <template v-slot="scope">
            <el-button type="primary" @click="showContent(scope.row)">查看订单详情</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="orderState" label="支付状态">
          <template v-slot="scope">
            <span v-if="scope.row.orderState">已支付</span>
            <span v-else>待支付</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="scope">
<!--            <el-button type="primary" @click="updateActivityForm(scope.row)">编辑</el-button>-->
            <el-button type="danger" @click="deleteOrder(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin: 10px 0">
      <div class="example-pagination-block">
        <el-pagination
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
        v-model="orderVisible"
        title="订单详情"
        width="60%"
    >
      <div class="w-e-text">
        <div class="order-details">
          <p><strong>订单名称:</strong> {{ order.goodsName }}</p>
          <p><strong>订单图片:</strong> <img :src="order.picture || 'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png'"
                                             alt="" style="width: 50px; height: 50px; border-radius: 50%; vertical-align: middle;"></p>
          <p><strong>订单数量:</strong> {{ order.number }}</p>
          <p><strong>订单价格:</strong> {{ order.price }}</p>
          <p><strong>订单号:</strong> {{ order.orderNum }}</p>
          <p><strong>创建时间:</strong> {{ order.createTime }}</p>
          <p><strong>支付编号:</strong> {{ order.payNum }}</p>
          <p><strong>支付时间:</strong> {{ order.payTime }}</p>
          <div>
            <p v-if="order.orderState"><strong>支付状态:</strong>已支付</p>
            <p v-else><strong>支付状态:</strong>待支付</p>
          </div>
        </div>
        <div slot="footer" style="text-align: right;">
          <el-button @click="orderVisible = false">确 定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
import E from "wangeditor";
import hljs from "highlight.js";

export default {
  name: "ShoppingCar",
  data() {
    return {
      orderVisible: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      name: '',
      order: {},
      user : JSON.parse(localStorage.getItem('honey-user')),

      rules: {

      }
    }
  },
  created() {
    this.load();
  },
  methods: {
    reset() { // 重置查询条件
      this.name = '';
      this.type = '';
      this.load();
    },
    load(pageNum) { // 分页查询
      if (pageNum) {
        this.pageNum = pageNum;
      }
      request.get('/order/selectByPage', {
        params: { pageNum: this.pageNum, pageSize: this.pageSize, goodsName: this.name}
      }).then(res => {
        if (res && res.data && typeof res.data === 'object') {
          this.tableData = res.data.list || [];
          this.total = res.data.total || 0;
          console.log("订单数据:", this.tableData);
        } else {
          console.error("无效的响应数据:", res);
          ElMessage.error('获取订单数据失败，请稍后再试');
        }
      }).catch(err => {
        console.error("获取订单数据失败:", err);
      });
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum);
    },

    deleteOrder(row) {
      console.log("删除活动:", row.id);
      ElMessageBox.confirm(
          `是否确定删除活动?`,
          '确认删除',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
      ).then(() => {
        request.delete(`/order/delete/${row.id}`).then(response => {
          if (response.code === 1) {
            ElMessage.success('删除成功');
            this.load();
          } else {
            ElMessage.error(response.msg);
          }
        });
      }).catch(() => {});
    },

    handleSelectionChange(rows) {
      this.ids = rows.map(item => item.id);
    },


    showContent(row) {
      this.orderVisible = true;
      this.order = row;
      console.log("订单详情:", this.order);
    },

  }
}
</script>

<style scoped>
.example-pagination-block + .example-pagination-block {
  margin-top: 10px;
}
</style>
